package com.zyl.longpicturebackend.exception;

import com.zyl.longpicturebackend.common.BaseResponse;
import com.zyl.longpicturebackend.common.ResultUtils;
import javassist.NotFoundException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * 全局异常处理器
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(BusinessException.class)
    public BaseResponse businessExceptionHandler(BusinessException e) {
        log.error("BusinessException", e);
        return ResultUtils.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    public BaseResponse runtimeExceptionHandler(RuntimeException e) {
        log.error("RuntimeException", e);
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR);
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public BaseResponse NotFoundHandler(NoHandlerFoundException e) {
        log.error("NoHandlerFoundException", e);
        return ResultUtils.error(ErrorCode.NOT_FOUND_ERROR);
    }


}
